<?php
// 初始化棋盘，19x19 的二维数组，0 表示空，1 表示黑子，2 表示白子
$board = array_fill(0, 19, array_fill(0, 19, 0));

// 处理落子请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $x = $_POST['x'];
    $y = $_POST['y'];
    $player = $_POST['player'];

    // 简单检查落子位置是否合法（仅检查是否为空）
    if ($board[$y][$x] == 0) {
        $board[$y][$x] = $player;
    }

    // 简单的胜负判断，以一方占满棋盘一半格子判定为胜
    $blackCount = 0;
    $whiteCount = 0;
    foreach ($board as $row) {
        foreach ($row as $cell) {
            if ($cell == 1) {
                $blackCount++;
            } elseif ($cell == 2) {
                $whiteCount++;
            }
        }
    }
    $totalCells = 19 * 19;
    if ($blackCount > $totalCells / 2) {
        $winner = "黑子获胜！";
    } elseif ($whiteCount > $totalCells / 2) {
        $winner = "白子获胜！";
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页版围棋游戏</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>网页版围棋游戏</h1>
    <div id="board">
        <?php
        for ($y = 0; $y < 19; $y++) {
            for ($x = 0; $x < 19; $x++) {
                $cellClass = 'cell';
                if ($board[$y][$x] == 1) {
                    $cellClass .= ' black';
                } elseif ($board[$y][$x] == 2) {
                    $cellClass .= ' white';
                }
                echo "<div class='$cellClass' data-x='$x' data-y='$y'></div>";
            }
        }
        ?>
    </div>
    <?php if (isset($winner)): ?>
        <div id="result">
            <p><?php echo $winner; ?></p>
        </div>
    <?php endif; ?>
    <form id="moveForm" method="post">
        <input type="hidden" id="x" name="x">
        <input type="hidden" id="y" name="y">
        <input type="hidden" id="player" name="player" value="1">
    </form>
    <script src="script.js"></script>
</body>

</html>